//
//  Ugc_HomePage_TbvHead_WX.m
//  ZITHER
//
//  Created by weixin on 2018/9/27.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "Ugc_HomePage_TbvHead_WX.h"
//#import "UgcHomePageVC.h"

@implementation Ugc_HomePage_TbvHead_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
        [self setMasonry];
    }
    return self;
}

-(void)setModelSub:(MyselfFollowListModelSub *)modelsub{
    
    _modelSub = modelsub;
    
    NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,modelsub.avatar];
    
    [self.PicUser sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    
    self.labUserName.text = modelsub.nickName;
    
    [self isLikeMe:[modelsub.followMeStatus isEqualToString:@"0"]];
//    if ( [modelsub.followMeStatus isEqualToString:@"0"]){
//        self.labContent.text = @"";
//        [self.labUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self.PicUser.mas_centerY).offset(0);
//            make.left.equalTo(self.PicUser.mas_right).offset(8);
//        }];
//    }else{
//        self.labContent.text = @"已关注我";
//
//        [self.labUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.PicUser).offset(0);
//            make.left.equalTo(self.PicUser.mas_right).offset(8);
//        }];
//    }
    if (modelsub.introduction.length != 0 ) {
        self.introduction.text = modelsub.introduction;
    }
//    self.InterestBtn.selected = ![modelsub.followStatus isEqualToString:@"1"]?NO:YES;
//
//    if (self.InterestBtn.selected) {
//        //加关注
//        self.InterestBtn.backgroundColor = [UIColor whiteColor];
//    }else{
//        //已关注
//        self.InterestBtn.backgroundColor = baseColorTheme;
//    }
    if ([modelsub.followStatus isEqualToString:@"0"]) {
        //加关注
        self.InterestBtn.backgroundColor = [UIColor whiteColor];
        self.InterestBtn.selected = YES;
    }else{
        //已关注
        self.InterestBtn.backgroundColor = baseColorTheme;
        self.InterestBtn.selected = NO;
    }
    //0:普通 1:PGC 2:商户 3:web用户 4:品牌 5.PGC
    if ([modelsub.userType isEqualToString:@"0"]) {
        self.State =0;
    }else if ([modelsub.userType isEqualToString:@"5"]){
        self.State =1;
    }else if ([modelsub.userType isEqualToString:@"2"]){
        self.State =0;
    }else if ([modelsub.userType isEqualToString:@"4"]){
        self.State =2;
    }
    
}

-(void)isLikeMe:(BOOL)type{
    if (type){
        self.labContent.text = @"";
        [self.labUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.PicUser.mas_centerY).offset(0);
            make.left.equalTo(self.PicUser.mas_right).offset(8);
        }];
    }else{
        self.labContent.text = @"已关注我";
        [self.labUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.PicUser).offset(0);
            make.left.equalTo(self.PicUser.mas_right).offset(8);
        }];
    }
}

-(void)setUI{
    [self addSubview:self.PicUser];
    [self addSubview:self.labUserName];
    [self addSubview:self.labContent];
    [self addSubview:self.InterestBtn];
    [self addSubview:self.introduction];
    [self addSubview:self.PicUserBrand];
    [self addSubview:self.PicUserPGC];
    [self addSubview:self.lineView];
}

-(void)setMasonry{
    [self.PicUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(20);
        make.left.equalTo(self).offset(20);
        make.width.height.mas_equalTo(50);
    }];
    [self.labUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PicUser).offset(0);
        make.left.equalTo(self.PicUser.mas_right).offset(8);
    }];
    [self.labContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labUserName.mas_bottom).offset(8);
        make.left.equalTo(self.PicUser.mas_right).offset(8);
    }];
    [self.InterestBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.PicUser.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-20);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(32);
    }];
    
}

-(void)setState:(UgcHomePageTbvHeadState)State{
    
    _State = State;

    switch (State) {
        case UgcHomePageTbvHeadStateNormal:{
            self.PicUserBrand.hidden = YES;
            self.PicUserPGC.hidden = YES;
            
            [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.PicUser.mas_bottom).offset(20);
                make.left.right.equalTo(self).offset(0);
                make.height.mas_equalTo(1);
            }];
            
            
        }
            
            break;
        case UgcHomePageTbvHeadStatePulling:{
            self.PicUserBrand.hidden = YES;
            self.PicUserPGC.hidden = NO;
            [self.PicUserBrand mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.labContent.mas_bottom).offset(10);
                make.left.equalTo(self.PicUser.mas_right).offset(0);
                make.width.mas_equalTo(0);
                make.height.mas_equalTo(16);
            }];
            
            [self.PicUserPGC mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.PicUserBrand.mas_centerY).offset(0);
                make.left.equalTo(self.PicUserBrand.mas_right).offset(5);
//                make.width.mas_equalTo(50);
//                make.height.mas_equalTo(20);
            }];
            
            [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.PicUserPGC.mas_bottom).offset(10);
                make.left.right.equalTo(self).offset(0);
                make.height.mas_equalTo(1);
            }];
        }
            
            break;
        case UgcHomePageTbvHeadStateBiz:{
            self.PicUserBrand.hidden = NO;
            self.PicUserPGC.hidden = NO;
            [self.PicUserBrand mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.labContent.mas_bottom).offset(0);
                make.left.equalTo(self.PicUser.mas_right).offset(0);
//                make.width.mas_equalTo(44);
//                make.height.mas_equalTo(16);
            }];
            
            [self.PicUserPGC mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.PicUserBrand.mas_centerY).offset(0);
                make.left.equalTo(self.PicUserBrand.mas_right).offset(10);
//                make.width.mas_equalTo(44);
//                make.height.mas_equalTo(16);
            }];
            
            [self.introduction mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.PicUserPGC.mas_bottom).offset(10);
                make.left.equalTo(self.mas_left).offset(15);
                make.right.equalTo(self.mas_right).offset(-15);

                //                make.width.mas_equalTo(44);
                //                make.height.mas_equalTo(16);
            }];
            
            [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.introduction.mas_bottom).offset(20);
                make.left.right.equalTo(self).offset(0);
                make.height.mas_equalTo(1);
            }];
     
        }
            break;
        default:
            break;
    }
    
    /** mssonry高度计算 */
    [self layoutIfNeeded];
    CGFloat Height = CGRectGetMaxY(self.lineView.frame);
    NSLog(@"1height = %f",Height);
    CGRect f = self.frame;
    f.size.height = Height+20;
    self.frame = f;
    self.height = Height+20;
    
    if (self.block) {
        self.block();
    }
    [self isNoMoreDate:self.isNoMoreDate];
    
}



-(BaseLab *)introduction
{
    if (!_introduction) {
        _introduction = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"" font:13 textColor:nil isBold:NO numberOfLines:0];
        _introduction.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
    }
    return _introduction;
}
-(BaseImageV *)PicUserBrand
{
    if (!_PicUserBrand) {
        _PicUserBrand = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        _PicUserBrand.image = [UIImage imageNamed:@"个人主页_入驻品牌"];
        [[_PicUserBrand addTag]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            NSLog(@"123");
            [self showBigImage:100];
        }];
    }
    return _PicUserBrand;
}
-(BaseImageV *)PicUserPGC
{
    if (!_PicUserPGC) {
        _PicUserPGC = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        _PicUserPGC.image = [UIImage imageNamed:@"个人主页_认证pgc"];
        [[_PicUserPGC addTag]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            NSLog(@"123");
            [self showBigImage:100];
        }];
    }
    return _PicUserPGC;
}

-(BaseImageV *)PicUser
{
    if (!_PicUser) {
        _PicUser = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        [_PicUser setCornerRadiusWithRadius:25 isMasks:YES];
        [[_PicUser addTag]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            NSLog(@"123");
            [self showBigImage:100];
        }];
    }
    return _PicUser;
}
-(BaseLab *)labUserName
{
    if (!_labUserName) {
        _labUserName = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"用户名称" font:18 textColor:nil isBold:NO numberOfLines:1];
        _labUserName.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
    }
    return _labUserName;
}
-(BaseLab *)labContent
{
    if (!_labContent) {
        _labContent = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"是否关注" font:14 textColor:baseColorLight isBold:NO numberOfLines:1];
        _labContent.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
        
    }
    return _labContent;
}
-(BaseBtn *)InterestBtn
{
    if (!_InterestBtn) {
        _InterestBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 1, 1) NormalText:@"已关注" NormalTextColor:[UIColor whiteColor] SeleText:@"关注" SeleTextColor:baseColorTheme  font:12 isBold:NO];
        _InterestBtn.backgroundColor = baseColorTheme;
        [_InterestBtn setCornerRadiusWithRadius:4 isMasks:YES];
        [_InterestBtn setBorderWithColor:baseColorTheme width:1];
        @weakify(self);
        [[_InterestBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            if (!kUSER_INFO_MANAGER.userIsLogin) {
                [Tools pushSinge];
            }
            
            
            x.selected = !x.selected;
            if (x.selected) {
                //加关注
                self.InterestBtn.backgroundColor = [UIColor whiteColor];
            }else{
                //已关注
                self.InterestBtn.backgroundColor = baseColorTheme;
            }
            
            
            if (!x.selected) {
                self.InterestBtn.backgroundColor = [UIColor whiteColor];
                NSString *str = nil;
                if (self.type == 1) {
                    str = self.modelSub.followUserId;
                }else{
                    str = self.modelSub.userId;
                }
                [[serviceDefualt loadAddUserFollowFollowUserId:str reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"关注成功"];
                        self.InterestBtn.backgroundColor = baseColorTheme;
                        
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.InterestBtn.selected = ! self.InterestBtn.selected;
                        if (self.InterestBtn.selected) {
                            //加关注
                            self.InterestBtn.backgroundColor = [UIColor whiteColor];
                        }else{
                            //已关注
                            self.InterestBtn.backgroundColor = baseColorTheme;
                        }
                    }
                }];
                
                
            }else{
                self.InterestBtn.backgroundColor = baseColorTheme;
                NSString *str = nil;
                if (self.type == 1) {
                    str = self.modelSub.followUserId;
                }else{
                    str = self.modelSub.userId;
                }
                
                
                [[serviceDefualt loadCancelUserFollowFollowUserId:str reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"取消关注成功"];
                        self.InterestBtn.backgroundColor = [UIColor whiteColor];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.InterestBtn.selected = ! self.InterestBtn.selected;
                        if (self.InterestBtn.selected) {
                            //加关注
                            self.InterestBtn.backgroundColor = [UIColor whiteColor];
                        }else{
                            //已关注
                            self.InterestBtn.backgroundColor = baseColorTheme;
                        }
                    }
                }];
                
            }
        }];
    }
    return _InterestBtn;
}


-(void)showBigImage:(NSInteger)selectIndex{
    UIImageView *imageV = self.PicUser;
    // self.model.fileList[selectIndex-100];
    
    if (!self.imagePreviewViewController) {
        self.imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
        self.imagePreviewViewController.imagePreviewView.delegate = self;
        
        // QMUIImagePreviewViewController 对于以 window 的方式展示的情况，默认会开启手势拖拽退出预览功能。
        // 如果使用了手势拖拽，并且退出预览时需要飞到某个 rect，则需要实现这个 block，在里面自己去 exit，如果不实现这个 block，退出动画会使用 fadeOut 那种
        
    }
    @weakify(self);
    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
        @strongify(self);
        UIImageView *imageV1 = self.PicUser;
        
        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:[imageV1 convertRect:imageV1.frame toView:nil]];
        
    };
    self.imagePreviewViewController.imagePreviewView.currentImageIndex =  selectIndex-100;// 默认查看的图片的 index
    
    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:[imageV convertRect:imageV.frame toView:nil] cornerRadius:imageV.layer.cornerRadius];
}


#pragma mark - <QMUIImagePreviewViewDelegate>

- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    return 1;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    // 模拟异步加载的情况
    zoomImageView.reusedIdentifier = @(index);
    [zoomImageView showLoading];

    UIImageView *imageV = self.PicUser;
    [zoomImageView hideEmptyView];
    zoomImageView.image = imageV.image;
    
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}

#pragma mark - <QMUIZoomImageViewDelegate>

- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
    //点击缩回
    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
}

-(BaseView *)lineView{
    if (!_lineView) {
        _lineView = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 1)];
        _lineView.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
    }
    return _lineView;
}

/** 没数据时候需要吧分割线当背景线 */
-(void)isNoMoreDate:(BOOL)type{
    self.isNoMoreDate = type;
    if (type) {
        self.lineView.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
        [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(21);
        }];
    }else{
        self.lineView.backgroundColor = [UIColor colorWithHex:@"eeeeee"];
        [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1);
        }];
    }
    [self layoutIfNeeded];

}
@end
